package sindconsole.misc.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import common.mybatis.Queries;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import sindconsole.misc.bean.OperationTrace;
import sindconsole.misc.dao.OperationTraceMapper;

import javax.annotation.Nullable;
import java.util.Date;

@Service
public class OperationServiceImpl implements OperationService {

    @Autowired
    OperationTraceMapper mapper;

    @Override
    public int createTrace(OperationTrace trace) {
        return mapper.insert(trace);
    }

    @Override
    public Page<OperationTrace> listTraces(@Nullable String operation, @Nullable Long userId, @Nullable Date loggedTime0, @Nullable Date loggedTime1, @Nullable String loggedAddr, int... pagination) {
        LambdaQueryWrapper<OperationTrace> criteria = new QueryWrapper<OperationTrace>().lambda();
        if(operation != null) criteria.like(OperationTrace::getOperation, operation);
        if(userId != null) criteria.like(OperationTrace::getUserId, userId);
        if(loggedTime0 != null) criteria.ge(OperationTrace::getLoggedTime, loggedTime0);
        if(loggedTime1 != null) criteria.lt(OperationTrace::getLoggedTime, loggedTime1);
        if(loggedAddr != null) criteria.like(OperationTrace::getLoggedAddr, loggedAddr);
        criteria.orderByDesc(OperationTrace::getId);
        return Queries.paginate(() -> mapper.selectList(criteria), pagination);
    }

}
